Back to Editorials CodeCraft β
  • Github
  • Instagram
< >

Path Shifter

TIME LIMIT = 1 SEC.

  • As with nearly everyone else, you like to hang out with your friends at the portico or at other places like the canteen after the last period. And as with nearly everyone else, you get lost in the conversation, laughs, and play and end up losing track of time. But the security guard has blown the first whistle and you know you will miss your bus if you go too late, so you start moving towards your bus. There are two sides that lead to your bus- the left side and the right side and they are represented by 0s and 1s. Example, the left side can be represented as 000111000000111 and the right side can be represented by 000000001110000. You can only travel on the 0s, not on the 1s. You can always switch between the left and right sides anytime during your journey. Your task is to find out whether you will be able to make it to the bus or not. NOTE: The length of both sides will be equal. The length is represented by the letter 'L'.
Input Output
The first line will contain the length of both the sides L.
The second line will contain the left side.
The third line will contain the right side.
Print "YES" if you can reach your bus, and "NO" if you cannot (without the double quotes).
Constraints
  • 1 ≤ L ≤ 100
Example Test Case
Input              Output
15
000111000000111
000000001110000
YES
Solution

#include <iostream> #include <string> using namespace std; int main() { int length,temp; int current_side = 0; // stores current side of the road, can be 0 or 1. string sides[2]; //two strings to represent two sides of the road bool can_cross = true; cin >> length; //take in length of path cin >> sides[0] >> sides[1]; //take in two sides of the road. for(int i=0;i<length;i++) { if(sides[current_side][i+1] == '1') { //if theres a block on the next square.. //switch sides. if(sides[ ((current_side+1)%2) ][ i ] == '1') { //if there is a block on the other side too then, //cannot switch to the other side. cout << "NO" << endl; can_cross = false; break; } else { current_side = (current_side+1) % 2; //if current_side is 0, ((0+1)%2) = 1 //if current_side is 1, ((1+1)%2) = 0 } } } if(can_cross) { cout << "YES" << endl; } return 0; }
  • #1 Thieves
  • #2 The Queue of Doubts
  • #3 Summation of Primes
  • #4 Spacious Maximus
  • #5 Samosas
  • #6 Reasonably Sound
  • #7 Product of Digits
  • #8 Prime Usernames
  • #9 Path Shifter
  • #10 Keypad Count
  • #11 Even Vowels
  • #12 Encryption
  • #13 Decryption
  • #14 Canteen Accountant
  • #15 Attendance Register
  • #16 Amazing Year

Get in touch

  • executives@codingstudio.club
  • +91 9010342360
  • Vignana Bharthi Instute of Technology
    Aushapur, Ghatkesar, Hyderabad, Telengana - India

© coding.Studio();